package com.complex.spring.advance.service.messagesource;

import java.util.Date;
import java.util.Locale;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.context.support.StaticMessageSource;

public class TestMessageSource {
	
	public static void main(String[] args) {
		//StaticMessageSource编程方式实现(不适合生产环境)
		StaticMessageSource staticMessageSource = new StaticMessageSource();
		staticMessageSource.addMessage("menu.file", Locale.US, "File{0} & {1,time}");
		staticMessageSource.addMessage("menu.edit", Locale.US, "Edit");
		//注意MessageFormat的使用
		System.out.println(staticMessageSource.getMessage("menu.file", new Object[]{"arg0",new Date()}, Locale.US));
		//找不到message时,返回默认值
		System.out.println(staticMessageSource.getMessage("menu.edit", null,"Edit111", Locale.US));
		
		//ResourceBundleMessageSource 标准的MessageSource
		ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
		resourceBundleMessageSource.setBasenames(new String[]{"bean/messages"});//从classpath加载资源文件
		System.out.println(resourceBundleMessageSource.getMessage("menu.file", new Object[]{"arg0",new Date()}, Locale.US));
		System.out.println(resourceBundleMessageSource.getMessage("menu.edit", null,"Edit111", Locale.US));
		System.out.println(resourceBundleMessageSource.getMessage("menu.file", new Object[]{"arg0",new Date()}, Locale.SIMPLIFIED_CHINESE));
		//中文需要使用ascii编码
		//native2ascii 1.txt 2.txt 进行编码转换
		System.out.println(resourceBundleMessageSource.getMessage("menu.edit", null,"Edit111", Locale.SIMPLIFIED_CHINESE));
		//ReloadableResourceBundleMessageSource 定期刷新properties文件是事变更
		ReloadableResourceBundleMessageSource reloadalbeMessageSource = new ReloadableResourceBundleMessageSource();
		reloadalbeMessageSource.setBasenames(new String[]{"bean/messages"});//最好从文件系统加载资源文件,这个reload才有意义 file:xxxx
		System.out.println(reloadalbeMessageSource.getMessage("menu.file", new Object[]{"arg0",new Date()}, Locale.US));
		System.out.println(reloadalbeMessageSource.getMessage("menu.edit", null,"Edit111", Locale.US));
		System.out.println(reloadalbeMessageSource.getMessage("menu.file", new Object[]{"arg0",new Date()}, Locale.SIMPLIFIED_CHINESE));
	}
}
